<template>
  <div>
    <el-row type="flex" justify="end" :gutter="20">
      <el-col :span="8">
        <!-- 年份的下拉菜单 -->
        <el-select v-model="currentYear" size="mini" @change="changeValue">
          <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
        </el-select>
      </el-col>
      <!-- 月份的下拉菜单 -->
      <el-col :span="8">
        <el-select v-model="currentMonth" size="mini" @change="changeValue">
          <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
        </el-select>
      </el-col>
    </el-row>
    <!-- 日历组件 -->
    <el-calendar v-model="value">
      <template #dateCell="{date,data}">
        <!-- {{ date }}  是一个日期对象 -->
        <!-- {{ data }} { "isSelected": false, "type": "current-month", "day": "2023-02-01" } -->
        <div class="date-content">
          <span class="text"> {{ +data.day.split('-')[2] }}</span>
          <!-- 周末休息 -->
          <!-- <span v-if="isRest(date)" class="rest">休</span> -->
          <!-- 排班表方式休息 -->
          <span v-if="isRest2(data.day)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value: new Date(), // 日历组件关联的数据
      currentMonth: '', // 月份
      currentYear: '', // 年份
      // 模拟排班表休息日的假数据
      restList: ['2023-02-01', '2023-02-11', '2023-02-21', '2023-02-22', '2023-02-23', '2023-02-25']
    }
  },
  computed: {
    yearList() {
      return Array.from(new Array(11), (item, index) => this.currentYear - 5 + index)
    }
  },
  watch: {
    //  监听value的变化得到新的日期对象后 处理得到最新的年和月赋值给currentMonth+currentYear
    value(newVal) {
      this.currentMonth = newVal.getMonth() + 1
      this.currentYear = newVal.getFullYear()
    }
  },
  created() {
    // console.log(new Date().getMonth() + 1) // 当前月份
    this.currentMonth = new Date().getMonth() + 1
    this.currentYear = new Date().getFullYear()
  },
  methods: {
    changeValue() {
      // 获取到最新的年份和月份 生成一个日期对象 交给日历组件关联的value
      // console.log(this.currentYear,this.currentMonth)
      this.value = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    },
    isRest(date) {
      return date.getDay() === 0 || date.getDay() === 6
    },
    isRest2(day) {
      // console.log(day)
      return this.restList.includes(day)
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-calendar__header{
  display: none;
}
::v-deep .el-calendar-day {
  height:  auto;
 }
::v-deep .el-calendar-table__row td::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  font-size: 12px;
  margin-left: 10px;
  padding: 3px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;

}
::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
::v-deep .el-calendar__header {
   display: none
 }
</style>
